package com.victor.controller.partner_controller;

import java.net.URLEncoder;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.victor.entity.Log;
import com.victor.entity.finefood.Food;
import com.victor.entity.finefood.FoodMeal;
import com.victor.entity.finefood.FoodMealPhoto;
import com.victor.entity.finefood.FoodPhoto;
import com.victor.entity.finefood.FoodPhotoGroup;
import com.victor.entity.finefood.FoodType;
import com.victor.service.PartnersService;
import com.victor.utils.DateUtils;
import com.victor.utils.Msg;

/**
 * 美食
 * 
 * @author zhangbin
 *
 */
@RestController
@RequestMapping("/partner/findfood")
public class Findfood_C {

	@Autowired
	private PartnersService service;

	// 获取美食店铺列表--通过分类id和搜索内容
	@RequestMapping("/getFoodList")
	public Msg getFoodList(String t_id, String search) {
		List<Food> foods = service.getFoodList(t_id, search);
		return Msg.success().add("foods", foods);
	}

	// 获取美食店铺详情
	@RequestMapping("/getFoodXQ")
	public Msg getFoodXQ(String f_id) {
		Food food = service.getFoodXQ(f_id);
		if (food != null) {

			return Msg.success().add("food", food);
		}
		return Msg.fail("未查询到相关信息");
	}

	// 删除评论
	@RequestMapping("/delComment")
	public Msg delComment(Integer c_id, String adminid) {
		boolean delResult = service.delFoodComment(c_id);
		if (delResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("删除了美食的评论");

			return Msg.success();
		}
		return Msg.fail("未查询到相关信息");
	}

	// 批量导出
	@RequestMapping("batchExport")
	public void batchExport(String adminid, String t_id, String search, HttpServletResponse response,
			HttpServletRequest request) throws Exception {
		// 获取美食列表--通过分类id和搜索内容
		List<Food> foods = service.getFoodList(t_id, search);

		// 创建poi导出数据对象
		SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook();
		// 创建sheet页
		Sheet sheet = sxssfWorkbook.createSheet("美食商家列表");

		CellRangeAddress region1 = new CellRangeAddress(0, 1, (short) 0, (short) 6);
		// 参数1：起始行 参数2：终止行 参数3：起始列 参数4：终止列
		sheet.addMergedRegion(region1);
		Row headTitle = sheet.createRow(0);
		headTitle.createCell(0).setCellValue("美食商家列表");

		// 创建表头
		Row headRow = sheet.createRow(4);
		// 设置表头信息
		headRow.createCell(0).setCellValue("id");
		headRow.createCell(1).setCellValue("名称");
		headRow.createCell(2).setCellValue("主题类别");
		headRow.createCell(3).setCellValue("位置--全");
		headRow.createCell(4).setCellValue("位置--省");
		headRow.createCell(5).setCellValue("位置--市");
		headRow.createCell(6).setCellValue("位置--区");
		headRow.createCell(7).setCellValue("位置--街道");
		headRow.createCell(8).setCellValue("位置--详细");
		headRow.createCell(9).setCellValue("位置备注");
		headRow.createCell(10).setCellValue("美食商家介绍");
		headRow.createCell(11).setCellValue("美食商家介绍视频");
		headRow.createCell(12).setCellValue("是否为首页推荐 0否 1是");
		headRow.createCell(13).setCellValue("美食商家类型表id");
		headRow.createCell(14).setCellValue("创建人id");
		headRow.createCell(15).setCellValue("是否冻结标志 0.正常 1.冻结");
		headRow.createCell(16).setCellValue("冻结起始时间");
		headRow.createCell(17).setCellValue("冻结终止时间");
		headRow.createCell(18).setCellValue("冻结周期 0.终身冻结 1.一小时, 2.一天, 3.一周, 4.一个月,5.一年,6.二年,7.三年,");
		headRow.createCell(19).setCellValue("冻结原由标题");
		headRow.createCell(20).setCellValue("冻结原由详情");

		// 遍历上面数据库查到的数据
		for (Food food : foods) {
			Row dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
			dataRow.createCell(0).setCellValue(food.getF_id());
			dataRow.createCell(1).setCellValue(food.getF_name());
			dataRow.createCell(2).setCellValue(food.getF_theme());
			dataRow.createCell(3).setCellValue(food.getF_position());
			dataRow.createCell(4).setCellValue(food.getF_position_province());
			dataRow.createCell(5).setCellValue(food.getF_position_city());
			dataRow.createCell(6).setCellValue(food.getF_position_area());
			dataRow.createCell(7).setCellValue(food.getF_position_street());
			dataRow.createCell(8).setCellValue(food.getF_position_detailed());
			dataRow.createCell(9).setCellValue(food.getF_position_remarks());
			dataRow.createCell(10).setCellValue(food.getF_introduce());
			dataRow.createCell(11).setCellValue(food.getF_video());
			dataRow.createCell(12).setCellValue(food.getF_ifhomepage());
			dataRow.createCell(13).setCellValue(food.getT_id());
			dataRow.createCell(14).setCellValue(food.getAdminid());
			dataRow.createCell(15).setCellValue(food.getF_frozen());
			dataRow.createCell(16).setCellValue(food.getF_frozen_starttime());
			dataRow.createCell(17).setCellValue(food.getF_frozen_endtime());
			dataRow.createCell(18).setCellValue(food.getF_frozen_cycle());
			dataRow.createCell(19).setCellValue(food.getF_frozen_title());
			dataRow.createCell(20).setCellValue(food.getF_frozen_details());

		}
		// 下载导出文件名
		String filename = "美食商家列表";
		// 设置头信息
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/vnd.ms-excel");
		// 一定要设置成xlsx格式
		response.setHeader("Content-Disposition",
				"attachment;filename=" + URLEncoder.encode(filename + ".xlsx", "UTF-8"));
		// 创建一个输出流
		ServletOutputStream outputStream = response.getOutputStream();
		// 写入数据
		sxssfWorkbook.write(outputStream);
		// 关闭
		outputStream.close();
		sxssfWorkbook.close();

		// 添加操作日志
		Log log = new Log();
		log.setDate(DateUtils.getNowDate());
		log.setTime(DateUtils.getNowTime());
		log.setAdminid(adminid);
		log.setContent("批量导出美食商家列表");
	}

	// 添加美食商家
	@RequestMapping("/addFood")
	public Msg addFood(Food food, String adminid) {
		boolean insResult = service.addFood(food);
		if (insResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("添加美食商家");

			return Msg.success();
		}
		return Msg.fail();
	}

	// 删除美食商家
	@RequestMapping("/delFood")
	public Msg delFood(Integer f_id, String adminid) {
		boolean delResult = service.delFood(f_id);
		if (delResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("删除美食商家");

			return Msg.success();
		}
		return Msg.fail();
	}

	// 获取所有类型
	@RequestMapping("/getFoodType")
	public Msg getFoodType() {
		List<FoodType> foodTypes = service.getFoodType();
		return Msg.success().add("foodTypes", foodTypes);
	}

	// 修改美食商家
	@RequestMapping("/updFood")
	public Msg updFood(Food food, String adminid) {
		boolean updResult = service.updFood(food);
		if (updResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("修改美食商家信息");
			return Msg.success();
		}
		return Msg.fail();
	}

	// 设置-取消 首页推荐 updHotel方法传id和推荐字段

	// 添加图片组
	@RequestMapping("/addFoodPhotoGroup")
	public Msg addFoodPhotoGroup(FoodPhotoGroup foodPhotoGroup, String adminid) {

		boolean insResult = service.addFoodPhotoGroup(foodPhotoGroup);
		if (insResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("美食商家添加图片组");
			return Msg.success();
		}
		return Msg.fail();
	}

	// 组内添加图片
	@RequestMapping("/addFoodPhoto")
	public Msg addFoodPhoto(FoodPhoto foodPhoto, String adminid) {

		boolean insResult = service.addFoodPhoto(foodPhoto);
		if (insResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("美食商家添加图片");
			return Msg.success();
		}
		return Msg.fail();
	}

	// 删除组
	@RequestMapping("/delFoodPhotoGroup")
	public Msg delFoodPhotoGroup(String g_id, String adminid) {
		boolean delResult = service.delFoodPhotoGroup(g_id);
		if (delResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("删除美食商家介绍图片组");
			return Msg.success();
		}
		return Msg.fail();
	}

	// 删除图片
	@RequestMapping("/delFoodPhoto")
	public Msg delFoodPhoto(Integer p_id, String adminid) {
		boolean delResult = service.delFoodPhoto(p_id);
		if (delResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("删除美食商家介绍图片");
			return Msg.success();
		}
		return Msg.fail();
	}

	// 获取美食列表(传美食商家id)
	@RequestMapping("/getFoodMealByFoodid")
	public Msg getFoodMealByFoodid(String f_id) {
		List<FoodMeal> foodMeals = service.getFoodMealByFoodid(f_id);

		return Msg.success().add("foodMeals", foodMeals);
	}

	// 获取美食详情
	@RequestMapping("/getFoodMealXQ")
	public Msg getFoodMealXQ(String m_id) {
		FoodMeal foodMeal = service.getFoodMealXQ(m_id);

		return Msg.success().add("foodMeal", foodMeal);
	}

	// 删除美食套餐
	@RequestMapping("/delMeal")
	public Msg delMeal(Integer m_id, String adminid) {

		boolean delResult = service.delMeal(m_id);
		if (delResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("删除美食套餐");
			return Msg.success();
		}
		return Msg.fail();
	}

	// 添加美食套餐
	@RequestMapping("/addMeal")
	public Msg addMeal(@RequestBody FoodMeal foodMeal, @RequestParam String adminid) {
		// 添加房间信息返回id
		boolean insResult = service.addMeal(foodMeal);
		if (insResult) {
			// 添加图片list
			if (foodMeal.getFoodMealPhotos() != null && foodMeal.getFoodMealPhotos().size() > 0) {
				for (FoodMealPhoto foodMealPhoto : foodMeal.getFoodMealPhotos()) {
					foodMealPhoto.setM_id(String.valueOf(foodMeal.getM_id()));
					service.addFoodMealPhoto(foodMealPhoto);
				}
			}
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("添加了美食套餐");

			return Msg.success();
		}

		return Msg.fail("添加房间信息失败");
	}

	// 修改美食套餐信息
	@RequestMapping("/updMeal")
	public Msg updMeal(FoodMeal foodMeal, String adminid) {
		boolean updResult = service.updMeal(foodMeal);

		if (updResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("修改美食套餐信息");
			return Msg.success();
		}
		return Msg.fail();
	}

	// 添加美食套餐图片
	@RequestMapping("/addMealPhoto")
	public Msg addMealPhoto(FoodMealPhoto foodMealPhoto, String adminid) {

		boolean insResult = service.addMealPhoto(foodMealPhoto);
		if (insResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("添加宾馆房间图片");
			return Msg.success();
		}
		return Msg.fail();
	}

	// 删除美食套餐图片
	@RequestMapping("/delMealPhoto")
	public Msg delMealPhoto(Integer p_id, String adminid) {

		boolean delResult = service.delMealPhoto(p_id);
		if (delResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("删除美食套餐图片");
			return Msg.success();
		}
		return Msg.fail();
	}

	// 冻结
	// frozen_title;// 冻结原由标题
	// frozen_details;// 冻结原由详情
	// frozen_starttime;//冻结开始时间
	// frozen_endtime;//冻结结束时间
	// frozen_cycle;// 冻结周期 0.终身冻结 1.1小时, 2.1天, 3.一周, 4.一个月,5.一年,6.二年,7.三年,
	@RequestMapping("/frozenFood")
	public Msg frozenFood(Integer f_id, String adminid, String frozen_cycle, String frozen_starttime,
			String frozen_endtime, String frozen_title, String frozen_details) {

		boolean updResult = service.frozenFood(f_id, frozen_cycle, frozen_starttime, frozen_endtime, frozen_title,
				frozen_details);
		if (updResult) {

			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("冻结了id为" + f_id + "的美食商家");
			return Msg.success();
		}
		return Msg.fail();
	}

}
